package com.douwong.student.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.douwong.student.R;
import com.douwong.student.activity.QuizShowActivity;
import com.douwong.student.adapter.AnswerExhangeAdapter;
import com.douwong.student.application.BaseApplication;
import com.douwong.student.db.AnswerRecordDBService;
import com.douwong.student.model.AnswerRecord;
import com.douwong.student.view.PullToRefreshListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 问答互动碎片
 * 
 * @author leihuajie
 * 
 */
@SuppressLint("ValidFragment")
public class AnswerExchangeFragment extends Fragment {
	private AnswerRecordDBService answerRecordDBService;
	private View view;
	private int[] textView = new int[] { R.id.all_text, R.id.single_text, R.id.mulps_text, R.id.judge_text,
			R.id.blank_text };

	private AnswerExhangeAdapter adapter;
	private LinearLayout all_ll, single_ll, mulps_ll, judge_ll, blank_ll;
	private PullToRefreshListView answer_listview;

	private ArrayList<AnswerRecord> list;
	private String MTAG = "AnswerExchangeFragment";
	private static Map<String, String> valueMap = new HashMap<String, String>();
	private String title = "";

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.answer_exchange_fragment_layout, null);
		initView();
		// HallTestModel model=JSONArray.parseObject(str, HallTestModel.class);
		// Log.e("JSON", model.getMsg()+"");

		adapter = new AnswerExhangeAdapter(getActivity(), 0, list);
		answer_listview.setAdapter(adapter);

		valueMap.put("1", "A");
		valueMap.put("2", "A,B");
		valueMap.put("3", "A-1,B-2,C-3,D-4");
		valueMap.put("4", "A,B");
		valueMap.put("5", "A");
		valueMap.put("6", "A,B");
		String ste = JSON.toJSONString(valueMap);
		Log.d("manmanmanman", ste);

		return view;
	}

	private void initView() {
		if (answerRecordDBService == null) {
			answerRecordDBService = new AnswerRecordDBService(getActivity());
		}
		all_ll = (LinearLayout) view.findViewById(R.id.all_ll);
		single_ll = (LinearLayout) view.findViewById(R.id.single_ll);
		mulps_ll = (LinearLayout) view.findViewById(R.id.mulps_ll);
		judge_ll = (LinearLayout) view.findViewById(R.id.judge_ll);
		blank_ll = (LinearLayout) view.findViewById(R.id.blank_ll);

		answer_listview = (PullToRefreshListView) view.findViewById(R.id.answer_listview);
		showTextView(textView[0]);
		answer_listview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

				// Toast.makeText(getActivity(), list.get(arg2).getTitleName(),
				// 3000).show();
				AnswerRecord record = list.get(arg2 - 1);
				Intent intent = new Intent();
				intent.putExtra("FilePath", record.getFilePath());
				intent.putExtra("TitleName", record.getTitleName());
				intent.putExtra("OptionStr", record.getOptionStr());
				intent.putExtra("StudentAnswer", record.getStudentAnswer());
				intent.putExtra("TrueAnswer", record.getTrueAnswer());
				String trueAnswer = record.getTrueAnswer();
				intent.putExtra("AwardCount", record.getAwardCount());
				intent.putExtra("Type", record.getType().getTip());
				intent.putExtra("CorrectRate", record.getCorrectRate());
				intent.setClass(getActivity(), QuizShowActivity.class);
				startActivity(intent);
			}
		});
		answer_listview.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				answer_listview.onScrollStateChanged(view, scrollState);
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
				answer_listview.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);

			}
		});
		answer_listview.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
			@Override
			public void onRefresh() {
				list.clear();
				if (BaseApplication.sUserBean != null) {
					if (title.equals("")) {
						list = answerRecordDBService.allAnswerRecord(BaseApplication.sUserBean.getUserbean().getEuNumber(), list);
					} else {
						list = answerRecordDBService.AnswerRecordType(BaseApplication.sUserBean.getUserbean().getEuNumber(), list,
								title);
					}
				}
				adapter.notifyDataSetChanged();
				answer_listview.onRefreshComplete();
			}


		});
		AnswerOnClick onClick = new AnswerOnClick();
		all_ll.setOnClickListener(onClick);
		single_ll.setOnClickListener(onClick);
		mulps_ll.setOnClickListener(onClick);
		judge_ll.setOnClickListener(onClick);
		blank_ll.setOnClickListener(onClick);
		list = new ArrayList<AnswerRecord>();
		if (BaseApplication.sUserBean != null) {
			answerRecordDBService.allAnswerRecord(BaseApplication.sUserBean.getUserbean().getEuNumber(), list);
		}

	}

	private class AnswerOnClick implements OnClickListener {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			// 全部
			case R.id.all_ll:
				showTextView(textView[0]);
				title = "";
				break;
			// 单选
			case R.id.single_ll:
				showTextView(textView[1]);
				title = "Single";
				break;
			// 多选
			case R.id.mulps_ll:
				showTextView(textView[2]);
				title = "Multiple";
				break;
			// 判断
			case R.id.judge_ll:
				showTextView(textView[3]);
				title = "Judge";
				break;
			// 填空
			case R.id.blank_ll:
				showTextView(textView[4]);
				break;
			default:
				break;
			}
			list.clear();
			if (BaseApplication.sUserBean != null) {
				if (title.equals("")) {
					list = answerRecordDBService.allAnswerRecord(BaseApplication.sUserBean.getUserbean().getEuNumber(), list);
				} else {
					list = answerRecordDBService.AnswerRecordType(BaseApplication.sUserBean.getUserbean().getEuNumber(), list,
							title);
				}
			}else{
				Toast.makeText(getActivity(), "请先登录", Toast.LENGTH_LONG).show();
			}

			adapter.notifyDataSetChanged();
		}

	}

	private void showTextView(int id) {
		for (int i = 0; i < textView.length; i++) {
			if (id == textView[i]) {
				view.findViewById(id).setVisibility(View.VISIBLE);
			} else {
				view.findViewById(textView[i]).setVisibility(View.INVISIBLE);
			}

		}
	}
}
